package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.LasyObejct;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * 雇员合同
 */
public class Bcode_Contract extends BaseJDBCPanel  {
	BaseStore<BusiObject> mainstore = null;

	public Bcode_Contract(BaseStore<BusiObject> mainstore) {
		this.mainstore = mainstore;
		init();
	}

	public Bcode_Contract() {
	}

	public void init() {

		initwidget();

	}

	private void initwidget() {
		VerticalPanel v = new VerticalPanel();
		v.setSpacing(5);

		FieldSet fs1 = new FieldSet();
		// fs1.setStyleName("firldset_css1");
		fs1.setHeading("合同列表");
		fs1.add(getgrid());

		FieldSet fs2 = new FieldSet();
		// fs2.setStyleName("firldset_css1");
		fs2.setHeading("添加合同");
		fs2.add(createForm());
		v.add(fs1);
		v.add(fs2);

		this.add(v);
		//return null;
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("contract_id");
		type.getField("contract_id").setType(String.class);
		type.addField("contract_name");
		type.getField("contract_name").setType(String.class);
		type.addField("contract_type");
		type.getField("contract_type").setType(String.class);
		type.addField("contract_begin_date");
		type.getField("contract_begin_date").setType(Date.class);
		type.addField("contract_end_date");
		type.getField("contract_end_date").setType(Date.class);
		type.addField("signed_date");
		type.getField("signed_date").setType(Date.class);
		type.addField("summary");
		type.getField("summary").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_bcode_contract");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_bcode_contract where bcode='{bcode}' and convert(varchar(10),contract_begin_date,120)< convert(varchar(10),contract_end_date,120)");
		info.setParent(mainstore);// 关联主表
		info.setLinkparentcolumns(new String[] { "bcode" }); // 关联主表字段
		info.setLinkparent_chile_columns(new String[] { "bcode" });// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGridPanel grid = null;

	BaseGridPanel getgrid() {
		createGrid();
		return grid;
	}

	void createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C3 = new ColumnConfig("contract_id", "合同编号", 80);
		columns.add(C3);

		ColumnConfig C4 = new ColumnConfig("contract_name", "合同名称", 80);
		columns.add(C4);

		ColumnConfig C5 = new ColumnConfig("contract_type", "合同类型", 80);
		BaseComboBoxForm<BaseModelData> b5 = new BaseComboBoxForm<BaseModelData>(
				"name", "name", "dictionary", "where type='hr_contract_type'",
				true);
		C5.setRenderer(b5);
		columns.add(C5);

		ColumnConfig C6 = new ColumnConfig("contract_begin_date", "合同起始日期", 90);
		C6.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C6);

		ColumnConfig C7 = new ColumnConfig("contract_end_date", "合同终止日期", 90);
		C7.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C7);

		ColumnConfig C8 = new ColumnConfig("signed_date", "合同签订日期", 90);
		DateField dateField8 = new DateField();
		dateField8.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C8.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C8);

		ColumnConfig C9 = new ColumnConfig("summary", "摘要", 170);
		columns.add(C9);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGridPanel(getStore(), cm, true, false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.getBg().setSelectionModel(cbs);
		grid.getBg().setWidth(730);
		grid.getBg().setHeight(120);
	}

	BaseFormPanel form = null;

	BaseFormPanel createForm() {
		form = new BaseFormPanel(getStore(), false);
		form.setBodyBorder(false);
		TableLayout tl = new TableLayout(6);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.MIDDLE);
		td.setPadding(2);

		TableData td2 = new TableData();
		td2.setColspan(5);
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.MIDDLE);
		td.setPadding(2);

		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("合同编号:"), td);
		TextField<String> C_num3 = new TextField<String>();
		C_num3.setWidth(127);
		C_num3.setName("contract_id");
		form.add(C_num3, td);
		form.add(new Label("合同名称:"), td);
		TextField<String> C_num4 = new TextField<String>();
		C_num4.setWidth(127);
		C_num4.setName("contract_name");
		form.add(C_num4, td);
		form.add(new Label("合同类型:"), td);
		BaseComboBoxForm<BaseModelData> b5 = new BaseComboBoxForm<BaseModelData>(
				"name", "name", "dictionary", "where type='hr_contract_type'",
				true);
		b5.setWidth(127);
		b5.setName("contract_type");
		form.add(b5, td);
		form.add(new Label("合同起始日期:"), td);
		DateField C_data6 = new DateField();
		C_data6.setWidth(127);
		C_data6.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-M-d"));
		C_data6.setName("contract_begin_date");
		form.add(C_data6, td);
		form.add(new Label("合同结束日期:"), td);
		DateField C_data7 = new DateField();
		C_data7.setWidth(127);
		C_data7.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-M-d"));
		C_data7.setName("contract_end_date");
		form.add(C_data7, td);
		form.add(new Label("合同签订日期:"), td);
		DateField C_data8 = new DateField();
		C_data8.setWidth(127);
		C_data8.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-M-d"));
		C_data8.setName("signed_date");
		form.add(C_data8, td);

		form.add(new Label("摘要:"), td);
		TextArea t9 = new TextArea();
		t9.setFireChangeEventOnSetValue(true);
		t9.setWidth(595);
		t9.setHeight(150);
		t9.setName("summary");
		form.add(t9, td2);
		form.inited();
		return form;
	};


}
